package com.service.thirddatacenter.hpool.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.service.thirddatacenter.hpool.model.entity.AccountThirdEntity;
import com.service.thirddatacenter.hpool.service.AccountThirdService;
import com.service.thirddatacenter.hpool.service.BasePlatformService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.service.thirddatacenter.hpool.mapper.AccountShareMapper;
import com.service.thirddatacenter.hpool.model.entity.AccountShareEntity;
import com.service.thirddatacenter.hpool.service.AccountShareService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 账号贡献
 *
 * @date 2021-06-08 15:41:03
 */
@Slf4j
@Service("accountShareService")
public class AccountShareServiceImpl extends ServiceImpl<AccountShareMapper, AccountShareEntity> implements AccountShareService {

    @Autowired
    private AccountThirdService accountThirdService;

    @Autowired
    private BasePlatformService hpoolService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void syncAccountShare() {
        List<AccountThirdEntity> accountThirdList = accountThirdService.listAllAccount();
        if (Objects.isNull(accountThirdList)) {
            log.info("暂无第三方平台账号，获取贡献完成");
            return;
        }
        accountThirdList.parallelStream().forEach(account->{
            switch (account.getPlatform()){
                case "hpool":
                    hpoolService.syncShare(account);
                    return;
                default:
                    log.info("{}未实现", account);
                    return;
            }
        });
    }

    @Override
    public AccountShareEntity getLastShareRecord(Integer accountId, String type) {
        return getOne(Wrappers.lambdaQuery(AccountShareEntity.class)
                .eq(AccountShareEntity::getAccountId, accountId)
                .eq(AccountShareEntity::getShareType, type)
                .orderByDesc(AccountShareEntity::getRecordTime).last("limit 1"));
    }
}
